function data_array = readdata(read_type);

% READDATA - import data array from a file or workspace variable
%
%    DATA = READDATA('txt') imports a 2-D numeric data array from
%    a tab-delimited ASCII text file.
%
%    DATA = READDATA('mat') saves the data array in a MATLAB MAT
%    file.  DATA can be any MATLAB array.
%
%    DATA = READDATA('var') exports the data array to a workspace
%    variable.  DATA can be any MATLAB array.

% By:   S.C. Molitor (smolitor@med.unc.edu)
% Date: April 14, 2000

% initialize output

data_array = [];

% global declarations

global DATA_PATH				% data file path

% default parameters

mat_filter = '*.mat';		% file filter for MAT files
txt_filter = '*.txt';		% file filter for TXT files

% validate arguments

if (nargin ~= 1)
   msgbox('Invalid number of arguments', 'READDATA Error', 'warn');
   return
elseif (~ischar(read_type) | isempty(read_type))
   msgbox('TYPE must be a character string', 'READDATA Error', 'warn');
   return
end

% change to specified path if valid

if (ischar(DATA_PATH) & ~strcmp(read_type, 'var'))
   if (exist(DATA_PATH) == 7)
      cd(DATA_PATH);
   end
end

% read data from file or workspace

switch (read_type)
   
case 'txt'		% import data from delimited ASCII file
   [data_name, temp_path] = uigetfile(txt_filter, 'Import Array from Text File');
   if ((data_name == 0) & (temp_path == 0))
      return
   else
      DATA_PATH = temp_path;
   end
   data_fid = fopen([DATA_PATH data_name], 'rt');
   if (data_fid == -1)
      msgbox(['Cannot open ' DATA_PATH data_name '.'], 'READDATA Error', 'warn');
      return
   end
   while (1)
      data_line = fgetl(data_fid);
      if (data_line == -1)
         break
      end
      data_vector = sscanf(data_line, '%f');
      data_array(end + 1, :) = data_vector';
   end
   status = fclose(data_fid);
   
case 'mat'		% import data from MATLAB MAT file
   [data_name, temp_path] = uigetfile(mat_filter, 'Import Array from Matlab File');
   if ((data_name == 0) & (temp_path == 0))
      return
   else
      DATA_PATH = temp_path;
   end
   cd(DATA_PATH);
   mat_struct = load(data_name);
   mat_varname = fieldnames(mat_struct);
   if (length(mat_varname) == 1)
      data_array = getfield(mat_struct, mat_varname{1});
   else
      data_array = mat_struct;
   end
   
case 'var'		% import data from workspace variable
   read_name = inputdlg({'variable name = '}, 'Import Array from Workspace', 1);
   if (~isempty(read_name))
      if (ischar(read_name{1}))
         data_array = evalin('base', read_name{1}, '[]');
      end
   end
   
end
return
